﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    public class LedgerOptController : ApiController
    {
        /// <summary>
        /// 获取实收实付信息
        /// eg.：0000000001|TST1|WEB|20
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|结算单编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/ledgeropt")]
        public RespLedger GetLedgerInfo(string key)
        {
            RespLedger resp = new RespLedger();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iFnid;
            if (!int.TryParse(strsKeyOriginal[3], out iFnid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetLedger(strMemberid, strComcd, iFnid);
            return resp;
        }

        /// <summary>
        /// 操作实收实付信息
        /// 只支持增删
        /// </summary>
        /// <param name="_reqInfo">实收实付请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/ledgeropt")]
        public RespFinaNullResultInfo UpdateLedger(ReqLedger _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateLedger(_reqInfo.memberid, _reqInfo.ledgerinfo);
            return resp;
        }





    }
}
